package com.sys.SysSales.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;


import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sys.common.annotation.Log;
import com.sys.common.core.controller.BaseController;
import com.sys.common.core.domain.AjaxResult;
import com.sys.common.enums.BusinessType;
import com.sys.SysSales.domain.SysSalesSummary;
import com.sys.SysSales.service.ISysSalesSummaryService;
import com.sys.common.utils.poi.ExcelUtil;
import com.sys.common.core.page.TableDataInfo;

/**
 * 销售统计Controller
 * 
 * @author ruoyi
 * @date 2025-09-28
 */
@RestController
@RequestMapping("/SysSales/SysSalesSummary")
public class SysSalesSummaryController extends BaseController
{
    @Autowired
    private ISysSalesSummaryService sysSalesSummaryService;

    /**
     * 查询销售统计列表
     */
    @PreAuthorize("@ss.hasPermi('SysSales:SysSalesSummary:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysSalesSummary sysSalesSummary)
    {
        startPage();
        List<SysSalesSummary> list = sysSalesSummaryService.selectSysSalesSummaryList(sysSalesSummary);
        return getDataTable(list);
    }

    /**
     * 导出销售统计列表
     */
    @PreAuthorize("@ss.hasPermi('SysSales:SysSalesSummary:export')")
    @Log(title = "销售统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysSalesSummary sysSalesSummary)
    {
        List<SysSalesSummary> list = sysSalesSummaryService.selectSysSalesSummaryList(sysSalesSummary);
        ExcelUtil<SysSalesSummary> util = new ExcelUtil<SysSalesSummary>(SysSalesSummary.class);
        util.exportExcel(response, list, "销售统计数据");
    }

    /**
     * 获取销售统计详细信息
     */
    @PreAuthorize("@ss.hasPermi('SysSales:SysSalesSummary:query')")
    @GetMapping(value = "/{summaryId}")
    public AjaxResult getInfo(@PathVariable("summaryId") Long summaryId)
    {
        return success(sysSalesSummaryService.selectSysSalesSummaryBySummaryId(summaryId));
    }

    /**
     * 新增销售统计
     */
    @PreAuthorize("@ss.hasPermi('SysSales:SysSalesSummary:add')")
    @Log(title = "销售统计", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSalesSummary sysSalesSummary)
    {
        return toAjax(sysSalesSummaryService.insertSysSalesSummary(sysSalesSummary));
    }

    /**
     * 修改销售统计
     */
    @PreAuthorize("@ss.hasPermi('SysSales:SysSalesSummary:edit')")
    @Log(title = "销售统计", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSalesSummary sysSalesSummary)
    {
        return toAjax(sysSalesSummaryService.updateSysSalesSummary(sysSalesSummary));
    }

    /**
     * 删除销售统计
     */
    @PreAuthorize("@ss.hasPermi('SysSales:SysSalesSummary:remove')")
    @Log(title = "销售统计", businessType = BusinessType.DELETE)
	@DeleteMapping("/{summaryIds}")
    public AjaxResult remove(@PathVariable Long[] summaryIds)
    {
        return toAjax(sysSalesSummaryService.deleteSysSalesSummaryBySummaryIds(summaryIds));
    }

    /**
     * 按日期统计销售数据
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @param monthFlag 月度统计标记
     * @param yearFlag  年度统计标记
     * @return 包含销售统计结果的 AjaxResult
     */
    @GetMapping("/date")
    public SysSalesSummary getSalesStat(
            @RequestParam(required = false) String beginTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Boolean dayFlag,
            @RequestParam(required = false) Boolean monthFlag,
            @RequestParam(required = false) Boolean yearFlag) {
        // 调用 Service 层方法获取销售统计数据
        return sysSalesSummaryService.getSalesStatByTime(beginTime, endTime, dayFlag, monthFlag, yearFlag);
    }

//
//    /**
//     * 按时间查询销量最高的商品
//     */
//    @GetMapping("/topProductName")
//    public SysSalesSummaryDto getTopSellingProduct(
//            @RequestParam(required = false) Boolean dayFlag,
//            @RequestParam(required = false) Boolean monthFlag,
//            @RequestParam(required = false) Boolean yearFlag,
//            @RequestParam(required = false) String beginTime,
//            @RequestParam(required = false) String endTime) {
//
////        // 直接将请求参数传递给Service层
////        String topProductName = sysSalesSummaryService.getTopSellingProduct(
////                dayFlag, monthFlag, yearFlag, beginTime, endTime
////        );
//
//        return sysSalesSummaryService.getTopSellingProduct(dayFlag, monthFlag, yearFlag,beginTime, endTime) ;
//    }

//    /**
//     * 按时间查询销量
//     */
//    @GetMapping("/totalQuantity")
//    public String getSalesVolumeByTimeRange(
//            @RequestParam(required = false) Boolean dayFlag,
//            @RequestParam(required = false) Boolean monthFlag,
//            @RequestParam(required = false) Boolean yearFlag,
//            @RequestParam(required = false) String beginTime,
//            @RequestParam(required = false) String endTime) {
//
//        // 直接将请求参数传递给Service层
//        String totalQuantity = sysSalesSummaryService.getSalesVolumeByTimeRange(
//                dayFlag, monthFlag, yearFlag, beginTime, endTime
//        );
//
//        return totalQuantity;
//    }


//    @PreAuthorize("@ss.hasPermi('SysSales:SysSalesSummary:lista')")
//    @GetMapping("/lista")
//    public TableDataInfo lista(SysSalesSummary sysSalesSummary)
//    {
//        startPage();
//        List<SysSalesSummary> lista = (List<SysSalesSummary>) sysSalesSummaryService.selectSysSalesSummaryLista(sysSalesSummary);
//        return getDataTable(lista);
//    }

}
